/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package windelpdv;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Servidor
 */
public class frmRetiradaCaixa extends javax.swing.JFrame {

    private Connection ConexaoPDVWindel = null;
    Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();

    /**
     * Creates new form frmRetiradaCaixa
     */
    public frmRetiradaCaixa(Connection conexaoPDVWindel) {
        this.ConexaoPDVWindel = conexaoPDVWindel;
        initComponents();
        //setSize(600, 600); //ponha aqui suas medidas  
        setLocation((tela.width - this.getSize().width) / 2, (tela.height - this.getSize().height) / 2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfValorRetirada = new javax.swing.JTextField();
        tfValorRetirada = new JMoneyField();
        btOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Retirada");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel1.setText("Retirada de Caixa");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel2.setText("Valor de Retirada:");

        tfValorRetirada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfValorRetiradaActionPerformed(evt);
            }
        });

        btOK.setText("OK");
        btOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(tfValorRetirada, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btOK))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel1)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfValorRetirada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btOK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfValorRetiradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfValorRetiradaActionPerformed
        Statement st = null;
        Date data = new Date(System.currentTimeMillis());
        SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");

        try {

            st = ConexaoPDVWindel.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                     

            st.execute("insert into MOVIMENTACAO (DATA, VALOR, TIPO) values ("
                    + "\'" + (formatarDate.format(data)) + "\',"+                    
                     "-"+tfValorRetirada.getText().replace(",", ".") + ","+
                                "\'" + "RETIRADA" + "\')" );            
            JOptionPane.showMessageDialog(null, "Valor retirado com sucesso!", "Atenção", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Registros não inseridos!", "Erro", JOptionPane.INFORMATION_MESSAGE);
        }
        
        /* Statement st = null;
        ResultSet res = null;
        Date data = new Date(System.currentTimeMillis());
        SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");

        try {

            st = ConexaoPDVWindel.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            res = st.executeQuery("SELECT MAX(COD_CUPOM) FROM FECHA_CAIXA");
            res.first();
            int Cod_cupom = res.getInt("MAX");
            Cod_cupom = Cod_cupom + 1;

            st.execute("insert into FECHA_CAIXA (DATA, DINHEIRO, CHEQUE, CREDIARIO, COD_CUPOM, TOTAL_CUPOM) values ("
                    + "\'" + (formatarDate.format(data)) + "\',"+
                    + +1 + ","
                    + +0 + ","
                    + +0 + ","
                    + +Cod_cupom + ","
                    + "-"+tfValorRetirada.getText().replace(",", ".") + ")");            
            JOptionPane.showMessageDialog(null, "Valor retirado com sucesso!", "Atenção", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Registros não inseridos!", "Erro", JOptionPane.INFORMATION_MESSAGE);
        }*/
    }//GEN-LAST:event_tfValorRetiradaActionPerformed

    private void btOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOKActionPerformed
        tfValorRetiradaActionPerformed(evt);
    }//GEN-LAST:event_btOKActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField tfValorRetirada;
    // End of variables declaration//GEN-END:variables
}
